<template>
  <div>
    <div style="margin-top: 10px; display: flex; justify-content: center">
      <el-input v-model="keywords" placeholder="通过用户名搜索用户" size="mini" prefix-icon="el-icon-search"
        style="width: 400px; margin-right: 10px"></el-input>
      <el-button size="mini" icon="el-icon-search" type="primary" @click="doSearch()">搜索</el-button>
    </div>
    <div class="hr-container">
      <el-card class="hr-card" v-for="(hr,index) in hrs" :key="index">
        <div slot="header" class="clearfix">
          <span>{{hr.name}}</span>
          <el-button style="float: right; padding: 3px 0;color:red;" type="text" icon="el-icon-delete"
            @click="deleHr(hr)">
          </el-button>
        </div>
        <div>
          <div class="img-div"><img :src="hr.userface" :alt="hr.name" :title="hr.name" class="userface-img">
          </div>
        </div>
        <div class="userinfo-container">
          <div>
            <div>用户名: {{hr.name}}</div>
            <div>手机号码: {{hr.phone}}</div>
            <div>电话号码: {{hr.telephone}}</div>
            <div>地址: {{hr.address}}</div>
            <div>用户状态: <el-switch v-model="hr.enabled" active-text="禁用" inactive-text="启用" active-color="#13ce66"
                inactive-color="#ff4949" @change="enabledChange(hr)">
              </el-switch>
            </div>
            <div>用户角色:
              <el-tag v-for="(role,index1) in hr.roles" size="mini" :key="index1" type="success" class="tags-container">
                {{role.nameZh}}
              </el-tag>
              <el-popover placement="right" title="用户角色" width="200" trigger="click" @show="showPop(hr)" @hide="hidePop(hr)">
                <el-select v-model="selectRoles" multiple placeholder="请选择">
                  <el-option
                    v-for="(r,indexr) in allRoles"
                    :key="indexr"
                    :label="r.nameZh"
                    :value="r.id">
                  </el-option>
                </el-select>
                <el-button slot="reference" icon="el-icon-more" size="mini" type="text"></el-button>
              </el-popover>
            </div>
            <div>备注: {{hr.remark}}</div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
  export default {
    components: {},
    data() {
      return {
        keywords: "",
        hrs: [],
        allRoles:[],
        selectRoles: []
      };
    },
    methods: {
      hidePop(hr) {
        let roleIds = [];//已有的roleid
        let roles = hr.roles;
        roles.forEach(item => {//先把已有的roleId放入数组
          roleIds.push(item.id);
        });
        if(roleIds.sort().toString() != this.selectRoles.sort().toString()) {//比较两个数组是否相等
          //不相等更新数据
          let obj = new Object();
          obj.hrId = hr.id;
          // obj.roleIds = [];
          obj.roleIds = this.selectRoles;
          this.putRequset("/system/hr/updateHrRole",JSON.parse(JSON.stringify(obj))).then(resp=> {
            this.initHrs();
          })
        }
      },
      showPop(hr) {
        let roles = hr.roles;
        this.selectRoles = [];
        roles.forEach(item => {
          this.selectRoles.push(item.id);
        });
        this.initAllRoles();
      },
      initAllRoles() {
        this.getRequset("/system/hr/roles").then((resp) => {
          if (resp) {
            this.allRoles = resp;
          }
        });
      },
      deleHr(hr) {
        this.$confirm(
          "此操作将永久删除【" + hr.name + "】管理员, 是否继续?", "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then(() => {
          this.deleteRequset("/system/hr/" + hr.id).then((resp) => {
            if (resp) {
              this.initHrs();
            }
          })
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        })
      },
      doSearch() {
        this.initHrs();
      },
      initHrs() {
        this.getRequset("/system/hr/?keywords=" + this.keywords).then((resp) => {
          if (resp) {
            this.hrs = resp;
          }
        });
      },
      enabledChange(hr) {
        let hr1 = new Object();
        hr1.id = hr.id;
        hr1.enabled = hr.enabled;
        // delete hr.roles;
        this.putRequset("/system/hr/", hr1).catch(resp => {
          this.initHrs();
        })
      }
    },
    mounted() {
      this.initHrs();
    }
  };
</script>
<style scoped>
  .userinfo-container {
    margin-top: 20px;
  }

  .userinfo-container div {
    color: #409eff;
    font-size: 12px;
  }

  .hr-container {
    margin-top: 10px;
    display: flex;
    /* 自动换行 */
    flex-wrap: wrap;
    /* 有间隔的分布 */
    justify-content: space-around;
  }

  .hr-card {
    width: 350px;
    margin-bottom: 20px;

  }

  .userface-img {
    width: 72px;
    height: 72px;
    border-radius: 72px;
  }

  .img-div {
    width: 100%;
    display: flex;
    /* 居中 */
    justify-content: center;
  }

  .tags-container {
    margin-left: 5px;
  }
</style>